const express = require('express');
const router = express.Router();
const TradeService = require('../services/TradeService');

// 创建交易
router.post('/', (req, res) => {
  try {
    const { symbol, type, quantity, price } = req.body;
    
    // 验证输入
    if (!symbol || !type || !quantity || !price) {
      return res.status(400).json({
        success: false,
        error: 'Symbol, type, quantity, and price are required'
      });
    }
    
    if (type !== 'BUY' && type !== 'SELL') {
      return res.status(400).json({
        success: false,
        error: 'Type must be either BUY or SELL'
      });
    }
    
    if (isNaN(quantity) || quantity <= 0) {
      return res.status(400).json({
        success: false,
        error: 'Quantity must be a positive number'
      });
    }
    
    if (isNaN(price) || price <= 0) {
      return res.status(400).json({
        success: false,
        error: 'Price must be a positive number'
      });
    }
    
    const trade = TradeService.createTrade(symbol, type, parseInt(quantity), parseFloat(price));
    
    res.status(201).json({
      success: true,
      data: trade
    });
  } catch (error) {
    res.status(500).json({
      success: false,
      error: error.message
    });
  }
});

// 执行交易
router.post('/:tradeId/execute', (req, res) => {
  try {
    const { tradeId } = req.params;
    const trade = TradeService.executeTrade(tradeId);
    
    res.json({
      success: true,
      data: trade
    });
  } catch (error) {
    res.status(500).json({
      success: false,
      error: error.message
    });
  }
});

// 取消交易
router.post('/:tradeId/cancel', (req, res) => {
  try {
    const { tradeId } = req.params;
    const trade = TradeService.cancelTrade(tradeId);
    
    res.json({
      success: true,
      data: trade
    });
  } catch (error) {
    res.status(500).json({
      success: false,
      error: error.message
    });
  }
});

// 获取特定交易
router.get('/:tradeId', (req, res) => {
  try {
    const { tradeId } = req.params;
    const trade = TradeService.getTrade(tradeId);
    
    if (!trade) {
      return res.status(404).json({
        success: false,
        error: 'Trade not found'
      });
    }
    
    res.json({
      success: true,
      data: trade
    });
  } catch (error) {
    res.status(500).json({
      success: false,
      error: error.message
    });
  }
});

// 获取所有交易
router.get('/', (req, res) => {
  try {
    const trades = TradeService.getAllTrades();
    
    res.json({
      success: true,
      data: trades
    });
  } catch (error) {
    res.status(500).json({
      success: false,
      error: error.message
    });
  }
});

// 根据股票代码获取交易
router.get('/symbol/:symbol', (req, res) => {
  try {
    const { symbol } = req.params;
    const trades = TradeService.getTradesBySymbol(symbol);
    
    res.json({
      success: true,
      data: trades
    });
  } catch (error) {
    res.status(500).json({
      success: false,
      error: error.message
    });
  }
});

module.exports = router;